package com.ruoyi.media.phoneCache.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.phoneCache.domain.bo.MediaCreateWechatMpPhoneBo;
import com.ruoyi.media.phoneCache.domain.vo.MediaCreateWechatMpPhoneVo;
import com.ruoyi.media.phoneCache.domain.MediaCreateWechatMpPhone;
import com.ruoyi.media.phoneCache.mapper.MediaCreateWechatMpPhoneMapper;
import com.ruoyi.media.phoneCache.service.IMediaCreateWechatMpPhoneService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 短信验证码号码池Service业务层处理
 *
 * @author dqw
 * @date 2023-05-06
 */
@RequiredArgsConstructor
@Service
public class MediaCreateWechatMpPhoneServiceImpl extends ServiceImpl<MediaCreateWechatMpPhoneMapper,MediaCreateWechatMpPhone> implements IMediaCreateWechatMpPhoneService {

    private final MediaCreateWechatMpPhoneMapper baseMapper;

    /**
     * 查询短信验证码号码池
     */
    @Override
    public MediaCreateWechatMpPhoneVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询短信验证码号码池列表
     */
    @Override
    public TableDataInfo<MediaCreateWechatMpPhoneVo> queryPageList(MediaCreateWechatMpPhoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaCreateWechatMpPhone> lqw = buildQueryWrapper(bo);
        Page<MediaCreateWechatMpPhoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询短信验证码号码池列表
     */
    @Override
    public List<MediaCreateWechatMpPhoneVo> queryList(MediaCreateWechatMpPhoneBo bo) {
        LambdaQueryWrapper<MediaCreateWechatMpPhone> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaCreateWechatMpPhone> buildQueryWrapper(MediaCreateWechatMpPhoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaCreateWechatMpPhone> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaCreateWechatMpPhone::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaCreateWechatMpPhone::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNumber()), MediaCreateWechatMpPhone::getPhoneNumber, bo.getPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneChace()), MediaCreateWechatMpPhone::getPhoneChace, bo.getPhoneChace());
        return lqw;
    }

    /**
     * 新增短信验证码号码池
     */
    @Override
    public Boolean insertByBo(MediaCreateWechatMpPhoneBo bo) {
        MediaCreateWechatMpPhone add = BeanUtil.toBean(bo, MediaCreateWechatMpPhone.class);
        add.setId(Long.parseLong(bo.getPhoneNumber()));
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改短信验证码号码池
     */
    @Override
    public Boolean updateByBo(MediaCreateWechatMpPhoneBo bo) {
        MediaCreateWechatMpPhone update = BeanUtil.toBean(bo, MediaCreateWechatMpPhone.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaCreateWechatMpPhone entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除短信验证码号码池
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
